# MarkEdit Makefile

# 变量定义
APP_NAME := markedit
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u '+%Y-%m-%d_%H:%M:%S')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")

# Go相关变量
GOCMD := go
GOBUILD := $(GOCMD) build
GOCLEAN := $(GOCMD) clean
GOTEST := $(GOCMD) test
GOGET := $(GOCMD) get
GOMOD := $(GOCMD) mod

# 构建相关变量
BUILD_DIR := build
BINARY_NAME := $(APP_NAME)
BINARY_UNIX := $(BINARY_NAME)_unix
BINARY_WINDOWS := $(BINARY_NAME).exe

# Docker相关变量
DOCKER_IMAGE := $(APP_NAME)
DOCKER_TAG := $(VERSION)

# LDFLAGS
LDFLAGS := -w -s \
	-X 'main.Version=$(VERSION)' \
	-X 'main.BuildTime=$(BUILD_TIME)' \
	-X 'main.GitCommit=$(GIT_COMMIT)'

.PHONY: all build clean test deps dev docker help

# 默认目标
all: clean deps test build

# 显示帮助信息
help:
	@echo "MarkEdit 构建工具"
	@echo ""
	@echo "可用命令:"
	@echo "  build      构建应用程序"
	@echo "  clean      清理构建文件"
	@echo "  test       运行测试"
	@echo "  deps       下载依赖"
	@echo "  dev        启动开发环境"
	@echo "  docker     构建Docker镜像"
	@echo "  docker-run 运行Docker容器"
	@echo "  deploy     部署到生产环境"
	@echo "  install    安装到系统"
	@echo "  cross      交叉编译"
	@echo "  lint       代码检查"
	@echo "  fmt        格式化代码"
	@echo "  mod        整理模块依赖"
	@echo "  help       显示此帮助信息"

# 下载依赖
deps:
	@echo "下载依赖..."
	$(GOMOD) download
	$(GOMOD) verify

# 整理模块依赖
mod:
	@echo "整理模块依赖..."
	$(GOMOD) tidy

# 运行测试
test:
	@echo "运行测试..."
	$(GOTEST) -v ./...

# 代码检查
lint:
	@echo "代码检查..."
	@if command -v golangci-lint >/dev/null 2>&1; then \
		golangci-lint run; \
	else \
		echo "golangci-lint 未安装，跳过代码检查"; \
		echo "安装命令: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"; \
	fi

# 格式化代码
fmt:
	@echo "格式化代码..."
	$(GOCMD) fmt ./...

# 构建应用程序
build: deps
	@echo "构建应用程序..."
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -a -installsuffix cgo -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_UNIX) ./cmd/server
	@echo "复制资源文件..."
	@cp -r config $(BUILD_DIR)/ 2>/dev/null || true
	@cp -r web $(BUILD_DIR)/ 2>/dev/null || true
	@echo "构建完成: $(BUILD_DIR)/$(BINARY_UNIX)"

# Windows构建
build-windows: deps
	@echo "构建Windows版本..."
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -a -installsuffix cgo -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_WINDOWS) ./cmd/server
	@echo "复制资源文件..."
	@cp -r config $(BUILD_DIR)/ 2>/dev/null || true
	@cp -r web $(BUILD_DIR)/ 2>/dev/null || true
	@echo "构建完成: $(BUILD_DIR)/$(BINARY_WINDOWS)"

# 交叉编译
cross: deps
	@echo "交叉编译..."
	@mkdir -p $(BUILD_DIR)
	# Linux
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -a -installsuffix cgo -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 ./cmd/server
	# Windows
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -a -installsuffix cgo -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe ./cmd/server
	# macOS
	CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -a -installsuffix cgo -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 ./cmd/server
	# ARM64
	CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GOBUILD) -a -installsuffix cgo -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 ./cmd/server
	@echo "交叉编译完成"

# 清理构建文件
clean:
	@echo "清理构建文件..."
	$(GOCLEAN)
	@rm -rf $(BUILD_DIR)
	@rm -rf tmp
	@rm -f build-errors.log

# 启动开发环境
dev: deps
	@echo "启动开发环境..."
	@mkdir -p logs uploads temp
	@if command -v air >/dev/null 2>&1; then \
		echo "使用 air 进行热重载..."; \
		air; \
	else \
		echo "直接运行（建议安装 air: go install github.com/cosmtrek/air@latest）"; \
		GIN_MODE=debug $(GOCMD) run ./cmd/server; \
	fi

# 构建Docker镜像
docker:
	@echo "构建Docker镜像..."
	docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) .
	docker tag $(DOCKER_IMAGE):$(DOCKER_TAG) $(DOCKER_IMAGE):latest
	@echo "Docker镜像构建完成: $(DOCKER_IMAGE):$(DOCKER_TAG)"

# 运行Docker容器
docker-run:
	@echo "运行Docker容器..."
	docker run -d -p 8080:8080 --name $(APP_NAME)-container $(DOCKER_IMAGE):latest

# 停止Docker容器
docker-stop:
	@echo "停止Docker容器..."
	docker stop $(APP_NAME)-container || true
	docker rm $(APP_NAME)-container || true

# 使用docker-compose
docker-compose-up:
	@echo "启动docker-compose..."
	docker-compose up -d

docker-compose-down:
	@echo "停止docker-compose..."
	docker-compose down

# 部署到生产环境
deploy: build
	@echo "部署到生产环境..."
	@if [ -f "scripts/deploy.sh" ]; then \
		chmod +x scripts/deploy.sh; \
		sudo scripts/deploy.sh; \
	else \
		echo "部署脚本不存在: scripts/deploy.sh"; \
	fi

# 安装到系统
install: build
	@echo "安装到系统..."
	@sudo cp $(BUILD_DIR)/$(BINARY_UNIX) /usr/local/bin/$(BINARY_NAME)
	@sudo chmod +x /usr/local/bin/$(BINARY_NAME)
	@echo "安装完成: /usr/local/bin/$(BINARY_NAME)"

# 卸载
uninstall:
	@echo "卸载..."
	@sudo rm -f /usr/local/bin/$(BINARY_NAME)
	@echo "卸载完成"

# 创建发布包
release: cross
	@echo "创建发布包..."
	@mkdir -p $(BUILD_DIR)/release
	@for binary in $(BUILD_DIR)/$(BINARY_NAME)-*; do \
		if [ -f "$$binary" ]; then \
			basename=$$(basename $$binary); \
			mkdir -p $(BUILD_DIR)/release/$$basename; \
			cp $$binary $(BUILD_DIR)/release/$$basename/; \
			cp -r config $(BUILD_DIR)/release/$$basename/ 2>/dev/null || true; \
			cp -r web $(BUILD_DIR)/release/$$basename/ 2>/dev/null || true; \
			cp README.md $(BUILD_DIR)/release/$$basename/ 2>/dev/null || true; \
			cd $(BUILD_DIR)/release && tar -czf $$basename.tar.gz $$basename/; \
			cd ../..; \
		fi \
	done
	@echo "发布包创建完成: $(BUILD_DIR)/release/"

# 显示版本信息
version:
	@echo "应用名称: $(APP_NAME)"
	@echo "版本: $(VERSION)"
	@echo "构建时间: $(BUILD_TIME)"
	@echo "Git提交: $(GIT_COMMIT)"